package com.vmove.childtrain.common;

import android.annotation.TargetApi;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.vmove.childtrain.R;
import com.vmove.childtrain.utils.AutoUtils;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import butterknife.ButterKnife;

public abstract class BaseActivity extends AppCompatActivity {

    protected Toolbar toolbar;
    protected TextView mTv_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        // 添加Activity到堆栈
        AppManager.getAppManager().addActivity(this);
        StatusBarUtil.setColor(this, 0x1b8ecc, 100);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setNavigationBarColor(getResources().getColor(R.color.colorPrimary));
        }
    }

    /**
     * 添加导航栏点击退出按钮
     * 前提：在调用{@link #initToolBar()}之后调用
     */
    protected void initBackNavigation(){
        toolbar.setNavigationIcon(R.drawable.ic_back_v);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        setTitle(returnToolBarTitle());
       /* if (null!=toolbar) {
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintResource(R.color.colorPrimary);//通知栏所需颜色
        }*/
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 结束Activity从堆栈中移除
//        AppManager.getAppManager().finishActivity(this);//防止6.0 点击后退键后直接退出app
    }

    protected void setTitle(String title) {
        if (null != toolbar&& !TextUtils.isEmpty(title)) {
            toolbar.setTitle("");
            mTv_title.setText(title);
        }
    }

    /**
     * 返回设置的标题
     * @return
     */
    public abstract String returnToolBarTitle();

    /**
     * 初始化ToolBar
     */
    public void initToolBar() {
        toolbar = (Toolbar) findViewById(R.id.tb_toolbar);
        if (toolbar != null) {
//            toolbar.setLogo(R.mipmap.ic_top);
            mTv_title = (TextView) findViewById(R.id.tv_title);
            toolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
//            toolbar.setTitleTextAppearance(this, R.style.ToolBarTitleTextApperance);
            toolbar.setTitleTextAppearance(this, R.style.ToolBarTitleTextApperance);
            toolbar.setTitle("");
            setSupportActionBar(toolbar);
        }
    }

    /**
     * 隐藏ToolBar
     */
    public void hideToolBar() {
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
    }

    /**
     * 显示ToolBar
     */
    public void showToolBar() {
        if (getSupportActionBar() != null) {
            getSupportActionBar().show();
        }
    }

    /**
     * 是否隐藏ToolBar Logo
     *
     * @param show
     */
    public void showOrHideToolBarLogo(boolean show) {
        if (show) {
            toolbar.setLogo(R.mipmap.ic_launcher);
        } else {
            toolbar.setLogo(null);
        }
    }


    public Toolbar getToolbar() {
        return toolbar;
    }

}
